package com.site.wisdom_construction.controller.healthEpidemicModel;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.entity.healthEpidemicModel.HealthCodeResult;
import com.site.wisdom_construction.entity.healthEpidemicModel.NucleicAcidReportResult;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.healthEpidemicModel.AntiepidemicInformationService;
import com.site.wisdom_construction.util.HttpRequest;
import com.site.wisdom_construction.util.TimeUtils;
import com.site.wisdom_construction.util.imgUtil.AliyunOSSUtil;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

/**
 * @Author daiyifei @Date 2022/1/7 @Description
 */
@Api(tags = "防疫健康信息接口")
@RestController
@RequestMapping("AntiepidemicInformation")
@CrossOrigin
public class AntiepidemicInformationController {
  @Autowired private AntiepidemicInformationService antiepidemicInformationService;

  @ApiOperation(value = "健康码图片上传", notes = "")
  @PostMapping("/healthCodeUpload")
  @NoToken
  public OneDataResult healthCodeUpload(MultipartFile file) {
    String uploadUrl = null;
    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "healthCode";
          // 上传到OSS
          uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    OneDataResult oneDataResult = new OneDataResult();
    HealthCodeResult healthCodeResult = new HealthCodeResult();
    healthCodeResult.setHealthCodePicture(uploadUrl);
    JSONObject jsonObject = new JSONObject();
    HttpRequest httpRequest = new HttpRequest();
    String path =
        "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise?access_token=24.e19537c168177d643a82901e7dea206d.2592000.1662185995.282335-25962476";
    jsonObject =
        httpRequest.StringDataJson(
            path, "url=" + uploadUrl + "&templateSign=f165961da3b7cc2adc4ac539320e7b32");
    JSONObject jsonObjectL = new JSONObject();
    jsonObjectL = (JSONObject) jsonObject.get("data");
    List list = (List) jsonObjectL.get("ret");
    for (int i = 0; i < list.size(); i++) {
      Map map1 = (Map) list.get(i);
      String s = (String) map1.get("word_name");
      if (s.equals("疫苗接种时间")) {
        String ss = (String) map1.get("word");
        if ((ss.substring(ss.length() - 1, ss.length())).equals("）")) {
          healthCodeResult.setVaccinationTime(ss.substring(0, ss.length() - 1));
        } else {
          healthCodeResult.setVaccinationTime(ss);
        }
      } else if (s.equals("疫苗接种情况")) {
        healthCodeResult.setVaccinationStatus((String) map1.get("word"));
      } else if (s.equals("健康码时间")) {
        String aaa = (String) map1.get("word");
        String a = aaa.replaceAll("：", ":");
        healthCodeResult.setHealthCodeTime(a);
      } else if (s.equals("健康码情况")) {
        String aa = (String) map1.get("word");
        if (aa.equals("未见异常")) {
          healthCodeResult.setHealthCodeStatus("绿码");
        } else {
          healthCodeResult.setVaccinationTime("黄码");
        }
      } else if (s.equals("行程码情况")) {
        String aa = (String) map1.get("word");
        if (aa.equals("您14天内未曾到访过中高风险地区所在的城市")) {
          healthCodeResult.setTourCodeStatus("正常");
        } else {
          healthCodeResult.setTourCodeStatus("异常");
        }
      }
    }
    oneDataResult.setData(healthCodeResult);
    return oneDataResult;
  }

  @ApiOperation(value = "核酸报告图片上传", notes = "")
  @PostMapping("/nucleicAcidReportUpload")
  @NoToken
  public OneDataResult nucleicAcidReportUpload(MultipartFile file) {
    String uploadUrl = null;
    try {

      if (null != file) {
        String filename = file.getOriginalFilename();
        if (!"".equals(filename.trim())) {
          File newFile = new File(filename);
          FileOutputStream os = new FileOutputStream(newFile);
          os.write(file.getBytes());
          os.close();
          file.transferTo(newFile);
          String Host = "nucleicAcidReport";
          // 上传到OSS
          uploadUrl = AliyunOSSUtil.upload(newFile, Host);
          // 删除上传时缓存在服务器上的文件
          newFile.delete();
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    OneDataResult oneDataResult = new OneDataResult();
    NucleicAcidReportResult nucleicAcidReportResult = new NucleicAcidReportResult();
    nucleicAcidReportResult.setDetectionPicture(uploadUrl);
    JSONObject jsonObject = new JSONObject();
    HttpRequest httpRequest = new HttpRequest();
    String path =
        "https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise?access_token=24.e19537c168177d643a82901e7dea206d.2592000.1662185995.282335-25962476";
    jsonObject =
        httpRequest.StringDataJson(
            path, "url=" + uploadUrl + "&templateSign=1dbb24fc7740977f234a2422a6d2e5c3");
    JSONObject jsonObjectL = new JSONObject();
    jsonObjectL = (JSONObject) jsonObject.get("data");
    List list = (List) jsonObjectL.get("ret");
    for (int i = 0; i < list.size(); i++) {
      Map map1 = (Map) list.get(i);
      String s = (String) map1.get("word_name");
      if (s.equals("姓名")) {
        nucleicAcidReportResult.setName((String) map1.get("word"));
      } else if (s.equals("核酸监测结果")) {
        nucleicAcidReportResult.setDetectionResult((String) map1.get("word"));
      } else if (s.equals("身份证号")) {
        nucleicAcidReportResult.setCard((String) map1.get("word"));
      } else if (s.equals("检测时间")) {
        nucleicAcidReportResult.setDetectionTime((String) map1.get("word"));
      }
    }
    oneDataResult.setData(nucleicAcidReportResult);
    return oneDataResult;
  }

  @ApiOperation(value = "增加防疫健康信息", notes = "测试数据:")
  @PostMapping("/insertAntiepidemicInformation")
  public JsonResult insertAntiepidemicInformation(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    map.put("updateTime", TimeUtils.getNowTimeS());

    int i = antiepidemicInformationService.insertAntiepidemicInformation(map);
    if (i == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功录入防疫健康信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("录入防疫健康信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "删除防疫健康信息", notes = "测试数据:")
  @PostMapping("/deleteAntiepidemicInformation")
  public JsonResult deleteAntiepidemicInformation(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    List<Integer> list = (List<Integer>) map.get("list");
    int l = 0;
    Map mapR = new HashMap();
    for (int i = 0; i < list.size(); i++) {
      mapR.put("id", list.get(i));
      int a = antiepidemicInformationService.deleteAntiepidemicInformation(mapR);
      l += a;
    }
    if (l >= 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功删除防疫健康信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("删除防疫健康信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "修改防疫健康", notes = "测试数据:")
  @PostMapping("/updateAntiepidemicInformation")
  public JsonResult updateAntiepidemicInformation(@RequestBody Map map) {
    JsonResult jsonResult = new JsonResult();
    map.put("updateTime", TimeUtils.getNowTimeS());
    int i = antiepidemicInformationService.updateAntiepidemicInformation(map);
    if (i == 1) {
      jsonResult.setCode(200);
      jsonResult.setMessage("成功修改防疫健康信息!");
    } else {
      jsonResult.setCode(20006);
      jsonResult.setMessage("修改防疫健康信息失败!");
    }
    return jsonResult;
  }

  @ApiOperation(value = "分页模糊查询防疫健康", notes = "测试数据:")
  @PostMapping("/findAntiepidemicInformationByPage")
  public Page findAntiepidemicInformationByPage(@RequestBody Map map) {
    Page page = new Page();
    page.setPageNo((Integer) map.get("pageNo"));
    page.setPageSize((Integer) map.get("pageSize"));
    page.setTotal(antiepidemicInformationService.Total(map));
    page.setItems(antiepidemicInformationService.findAntiepidemicInformationByPage(map));
    return page;
  }
}
